﻿///<reference path="jquery.js">
(function(){

if (window.Ctn){
    window._Ctn = window.Ctn;
}
window.Ctn = jQuery;
Ctn.version = '2.0';
//扩展字符串String对象
Ctn.extend(String.prototype, {
    left:function(l){
        return this.substr(0, l);
    },
    right:function(l){
        return this.substr(this.length - l, l);
    },
    lTrim:function(v){
        v = v == null ? " \t\r\n" : v;
        var a = v.split("");
        for (var i = 0, l = this.length; i < l; i++)
        {
            if (!a.contains(this.charAt(i))){
                return this.substring(i);
            }
        }
        return "";
    },
    rTrim:function(v){
        v = v == null ? " \t\r\n" : v;
        var a = v.split("");
        for (var i = this.length - 1; i >= 0; i--)
        {
            if (!a.contains(this.charAt(i))){
                return this.substring(0, i + 1);
            }
        }
        return "";
    },
    trim:function(v){
        return this.lTrim(v).rTrim(v);
    },
    toParams:function(){
        var s, a, o = {}, t, n, v, l=0;
        if (this.indexOf('?') > -1){
            s = this.split('?')[1];
        }else{
            s = this;
        }
        a = s.split('&');
        for (var i = 0; i < a.length; i++){
            t = a[i].split('=');
            n = t[0];
            v = t[1];
            if (v == null){
                o[l++] = n;
            }else if(o[n] != null){
                //如果已经是数组
                if (o[n] instanceof Array)
                {
                    o[n].push(v);
                }else{//如果不是数组则改为数组存储
                    t = o[n];
                    o[n] = [];
                    o[n].push(t);
                    o[n].push(v);
                }
            }else{
                o[n] = v;
            }
        }
    },
    //验证
    validate:function(){},
    startsWith:function(v){
        return this.indexOf(v) == 0;
    },
    endsWidth:function(v){
        return this.lastIndexOf(v) == this.length - v.length;
    }
});
//扩展数组Array对象
Ctn.extend(Array.prototype, {
    //遍历
    each: function(fn) {
        for (var i = 0, l = this.length; i < l; i++){
            fn(this[i]);
        }
    },
    from: function(v){
        for (var i = 0, l = v.length; i < l; i++){
            this[i] = v[i];
        }
        return this;
    },
    //转换为Int
    toInt : function(){
        var ret = [];
        for (var i = this.length - 1; i >= 0; i--){
            ret[i] = parseInt(this[i]);
        }
        return ret;
    },
    //查找
    indexOf:function(v){
        for (var i = 0, l = this.length; i < l; i++)
        {
            if (this[i] == v)
            {
                return i;
            }
        }
        return -1;
    },
    //包含
    contains:function(v){
        if (this.indexOf(v) > -1){
            return true;
        }else{
            return false;
        }
            
    },
    //克隆数组
    clone:function(){
        var ret = [];
        for (var i = 0, l = this.length; i < l; i++)
        {
            ret.push(this[i]);
        }
        return ret;
    },
    //清空
    clear:function(){
        this.length = 0;
        return this;
    },
    //移除
    remove:function(v){
        for (var i = this.length - 1; i >= 0; i--){
            if (this[i] == v){
                this.splice(i, 1);
            }
        }
        return this;
    },
    //移除
    removeAt:function(i){
        return this.splice(i, 1)[0];
    },
    //插入
    insertAt:function(i, v){
        for (var j = this.length - 1; j >= idx; j--)
        {
            this[j + 1] = this[j];
        }
        this[i] = v;
        return this;
    },
    //压缩
    compact: function() {
        var ret = [];
        for (var i = 0,l = this.length; i < l; i++)
        {
            if (this[i] != null){
                ret.push(this[i]);
            }
        }
        return ret;
    },
    //去除相同项
    unique: function(){
        var ret = [];
        for (var i = 0,l = this.length; i < l; i++){
            if (!ret.contains(this[i])){
                ret.push(this[i]);
            }
        }
        return ret;
    },
    //筛选
    grep: function(fn){
        var ret = [];
        for (var i = 0, l = this.length; i < l; i++){
            if(fn(this[i])){
                ret.push(this[i]); 
            }
        }
        return ret;
    },
    //遍历构造新数组
    map: function(fn){
        var v, ret = [];
        for (var i = 0, l = this.length; i < l; i++){
            v = fn(this[i]);
            if(v != null){
                ret.push(v); 
            }
        }
        return ret;
    }
});
//扩展数字Number对象
Ctn.extend(Number.prototype, {});
//扩展Boolean对象
Ctn.extend(Boolean.prototype, {
    from: function(v){
        v = typeof(v) == "string" ? v.toLowerCase() : v;
        if (v == false || v == 0 || v == "0" || v == "false"){
            return false;
        }else if(v == true || v == 1 || v == "1" || v == "true"){
            return true;
        }else{
            return this;
        }
    }
});
//扩展Object对象
Ctn.extend(Object.prototype, {});
//扩展函数Function对象
Ctn.extend(Function.prototype, {
    bind:function(){
        var __method = this, args = new Array().from(arguments), object = args.shift();
        return function() {
            return __method.apply(object, args.concat(new Array().from(arguments)));
        }
    }
});
//扩展日期Date对象
Ctn.extend(Date.prototype, {
});
//扩展数学Math对象
Ctn.extend(Math, {
});
//扩展错误Error对象
Ctn.extend(Error, {
    verify:function(){
    },
    show: function(){
    }
});
//对浏览器的扩展
Ctn.extend(Ctn.browser,{
    //msie
    ie:function(v){
        return this.msie && this.ver(v);
    },
    //firefox
    ff:function(v){
        return this.firefox && this.ver(v);
    },
    //safari
    sf:function(v){
        return this.safari && this.ver(v);
    },
    //opera
    op:function(v){
        return this.opera && this.ver(v);
    },
    //比较版本号
    ver:function(v){
        //不写版本号
        if (!v) {
            return true;
        }
        v = v.toString();
        var a,b,c,l,f;
        a = v.substring(v.length - 1);
        if (a == "+" || a == "-"){
            b = this.version.split(".");
            v = v.substring(0, v.length - 1);
            c = v.split(".");
            l = b.length > c.length ?c.length:b.length;
            f = a == "+"?true:false;
            for (var i = 0; i < l; i++){
                if ((f && c[i] > b[i]) || (!f && c[i] < b[i])){
                    return false;
                }
            }
            return true;
        }else{
            return this.version.indexOf(v) == 0;
        }
    }
});
})();